﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using DataBroker.Criteria;
using DataBroker.Dal;
using DataBroker.Dao;
using NorthwindDAO;
using NUnit.Framework;

namespace D2BkTest
{
    [TestFixture]
    public class FetchTests
    {
            [SetUp]
            public void SetUp()
            {
                D2Bk.ChangeDatabase("Northwind"); 
            }
        [Test]
        public void SortArrayWithConverter()
        {
            var customers = D2Bk.Fetch(new Filter(), new Customers());
            //since C# lists are invariant we need to convert each item
            List<Customers> list = customers.ConvertAll(value => (Customers)value);
            Array.Sort(list.ToArray(), (x, y) => x.Companyname.CompareTo(y.Companyname));
        }
        [Test]
        public void SortArrayWithExtensionClass()
        {
            var customers = D2Bk.Fetch(new Filter(), new Customers());
            customers.SortBy(x => ((Customers)x).Companyname);
        }
        [Test]
        public void SimpleBinding()
        {
            var sc = new Filter();
            sc.Add("Customerid", "ALFKI");
            var customers = D2Bk.Fetch(sc, new Customers());
            Assert.AreEqual(1, customers.Count);
        }
        [Test]
        public void AndBinding()
        {
            var sc = new Filter();
            sc.Add("Contacttitle", "Sales Agent");
            sc.Add("city", "london");
            var customers = D2Bk.Fetch(sc, new Customers());
            Assert.AreEqual(1, customers.Count);
        }

        [Test]
        public void OrBinding()
        {
            var sc = new Filter();
            sc.Add("Contacttitle", "Sales Agent");
            sc.Add("city", "london", Logical.Or);
            var customers = D2Bk.Fetch(sc, new Customers());
            Assert.AreEqual(10, customers.Count);
        }
        [Test]
        public void CallSp()
        {
            DataSet ds = D2Bk.Fetch("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
        }
        [Test]
        public void ReadWithEvent()
        {
            ReadWithEvent(null);
        }

        public void ReadWithEvent(object dummy)
        {
            D2Bk.ChangeDatabase("Northwind");
            D2Bk.ReadEntity += SQL_ReadEntity;
            D2Bk.Fetch(new Filter(), new Customers());
        }

        [Test]
        public void ReadWithoutEvent()
        {
            ReadWithoutEvent(null);
        }
        public void ReadWithoutEvent(object dummy)
        {
            D2Bk.ChangeDatabase("Northwind");
            D2Bk.Fetch(new Filter(), new Customers());
        }
        bool SQL_ReadEntity(Entity readEntity)
        {
            var customer = readEntity as Customers;
            if (customer != null) Console.WriteLine("Customer ID: {0}", customer.Customerid);
            return true;
        }

        bool SQL_ReadEntityStop(Entity readEntity)
        {
            var customer = readEntity as Customers;
            if (customer != null)
            {
                Console.WriteLine("Customer ID: {0}", customer.Customerid);
                if (customer.Customerid == "ALFKI")
                    return false;
            }
            return true;
        }


        [Test]
        public void ReadWithEventStop()
        {
            D2Bk.ReadEntity += SQL_ReadEntityStop;
            D2Bk.Fetch(new Filter(), new Customers());
        }

        [Test]
        public void MultiThread()
        {
            ThreadPool.QueueUserWorkItem(ReadWithoutEvent);
            ThreadPool.QueueUserWorkItem(ReadWithEvent);
            Thread.Sleep(2000);
        }
        [Test]
        public void CallSpMultipleTimes()
        {
            DataSet ds = D2Bk.Fetch("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
            ds = D2Bk.Fetch("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
            ds = D2Bk.Fetch("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
        }
        [Test]
        public void CallSpMultipleTimesForDataSet()
        {
            DataSet ds = D2Bk.ExecuteDataSetFromSP("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
            ds = D2Bk.ExecuteDataSetFromSP("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
            ds = D2Bk.ExecuteDataSetFromSP("CustOrdersDetail", 10248);
            Assert.AreEqual(3, ds.Tables[0].Rows.Count);
        }
        [Test]
        public void CallVariousSpMultipleTimesForDataSet()
        {
            for (int i = 0; i < 10; i++)
            {
                DataSet ds = D2Bk.ExecuteDataSetFromSP("CustOrdersDetail", 10248);
                Assert.AreEqual(3, ds.Tables[0].Rows.Count);
                ds = D2Bk.ExecuteDataSetFromSP("CustOrderHist", "ALFKI");
                Assert.AreEqual(11, ds.Tables[0].Rows.Count);
                ds = D2Bk.ExecuteDataSetFromSP("CustOrdersOrders", "ALFKI");
                Assert.AreEqual(6, ds.Tables[0].Rows.Count);
            }
        }
        [Test]
        public void CallVariousSpMultipleTimes()
        {
            for (int i = 0; i < 10; i++)
            {
                DataSet ds = D2Bk.Fetch("CustOrdersDetail", 10248);
                Assert.AreEqual(3, ds.Tables[0].Rows.Count);
                ds = D2Bk.Fetch("CustOrderHist", "ALFKI");
                Assert.AreEqual(11, ds.Tables[0].Rows.Count);
                ds = D2Bk.Fetch("CustOrdersOrders", "ALFKI");
                Assert.AreEqual(6, ds.Tables[0].Rows.Count);
            }
        }
    }
}
